/*
Created by =BTC= Giallustio
version 0.94 Offical release
Visit us at: 
http://www.blacktemplars.altervista.org/
06/03/2012
*/
//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "btc_revive_init.sqf"
#include <functions\btc_macros.h>

if (isServer) then
{
	private ["_vec","_var_name","_inc","_side","_str_array","_array","_i"];
	//Mobile
	BTC_vehs_mobile_west_str = [];
	BTC_vehs_mobile_east_str = [];
	BTC_vehs_mobile_guer_str = [];
	BTC_vehs_mobile_civ_str = [];
	if (BTC_active_mobile == 1) then {
		{
			_side = _x;
			_str_array = [];
			_array = missionNamespace getVariable [format["BTC_vehs_mobile_%1",_side],[]];
			if (count _array != 0) then {
				for "_i" from 0 to ((count _array) - 1) do {
					_veh = (_array select _i);
					_var = str(_veh);
					_str_array = _str_array + [_var];
					_resp = [_veh,_var,_side] spawn FUNC(vehicleMobileRespawn);
				};
			};
			missionNamespace setVariable [format["BTC_vehs_mobile_%1_str",_side],_str_array];
		} foreach ["west","east","guer","civ"];

		// Check if we added mobile vehicles through the editor
		_addMobileIfExists = {
			private ["_vec","_var_name","_side"];
			_var_name = _this select 0;
			_side = _this select 1;
			_vec = missionNamespace getVariable [_var_name,objNull];
			if (!isnil "_vec" && {typename _vec == typename objNull} && {!isnull _vec} && {(_vec isKindOf "AllVehicles" || _vec isKindOf "Building")}) then {
				BTC_vehs_mobile_west_str = BTC_vehs_mobile_west_str + [_var_name];
				[_vec,_var_name,_side] spawn FUNC(vehicleMobileRespawn);
			} else {
				// TRACE_1("Invalid Mobile Var Name",ARR_2(_var_name,_vec));
			};
		};
		
		for "_inc" from 0 to (49) step 1 do {
			{
				_side = _x;
				_var_name = format["BTC_Mobile_%1_%2",_side,_inc];
				[_var_name,_side] call _addMobileIfExists;
			} foreach ["west","east","guer","civ"];
		};
	};
	// TYRGHEN you need to publish it in all cases otherwise you get errors on the clients
	publicVariable "BTC_vehs_mobile_west_str";publicVariable "BTC_vehs_mobile_east_str";publicVariable "BTC_vehs_mobile_guer_str";publicVariable "BTC_vehs_mobile_civ_str";
	TRACE_1("Mobile Vecs",ARR_4(BTC_vehs_mobile_west_str,BTC_vehs_mobile_east_str,BTC_vehs_mobile_guer_str,BTC_vehs_mobile_civ_str));
	//
	BTCR_pveh_vec = [];publicVariable "BTCR_pveh_vec";
	BTCR_pveh_killed = [];publicVariable "BTCR_pveh_killed";
	BTCR_pveh_drag = [];publicVariable "BTCR_pveh_drag";
	BTCR_pveh_carry = [];publicVariable "BTCR_pveh_carry";
	BTCR_pveh_marker = [];publicVariable "BTCR_pveh_marker";
	BTCR_pveh_load = [];publicVariable "BTCR_pveh_load";
	BTCR_pveh_pullout = [];publicVariable "BTCR_pveh_pullout";
	if (BTC_r_new_system == 1) then 
	{
		BTC_anim_pveh = [];publicVariable "BTC_anim_pveh";
		BTC_cpr_pveh = [];publicVariable "BTC_cpr_pveh";
		BTC_ban_pveh = [];publicVariable "BTC_ban_pveh";
		BTC_med_pveh = [];publicVariable "BTC_med_pveh";
	};
	TRACE("SERVER REVIVE STARTED");
};

// Headless client initialization if any
if (!hasInterface) then {

};

if (isDedicated || !hasInterface) exitWith {};

//Init
[] spawn
{
	private ["_vec","_var_name","_inc","_side","_str_array","_array","_i"];
	waitUntil {!isNull player};
	waitUntil {player == player};
	"BTCR_pveh_vec" addPublicVariableEventHandler FUNC(PVEH);
	"BTCR_pveh_drag" addPublicVariableEventHandler FUNC(PVEH);
	"BTCR_pveh_carry" addPublicVariableEventHandler FUNC(PVEH);
	"BTCR_pveh_marker" addPublicVariableEventHandler FUNC(PVEH);
	"BTCR_pveh_load" addPublicVariableEventHandler FUNC(PVEH);
	"BTCR_pveh_pullout" addPublicVariableEventHandler FUNC(PVEH);
	"BTCR_pveh_killed" addPublicVariableEventHandler FUNC(PVEH);
	BTC_r_mobile_selected = objNull;
	BTC_r_bleeding = 0;
	BTC_r_bleeding_loop = false;
	player addRating 9999;
	BTC_r_list_names = [];
	BTC_r_list_objects = [];
	BTC_side = playerSide;
	BTC_side_str =  switch(toUpper(str(BTC_side))) do {
		case "EAST" : { "east" };
		case "OPFOR" : { "east" };
		case "GUER" : { "guer" };
		case "RESISTANCE" : { "guer" };
		case "INDEPENDENT" : { "guer" };
		case "CIV" : { "civ" };
		case "CIVILIAN" : { "civ" };
		default { "west" };
	};
	BTC_r_s_cam_view = [-15,-15,15];
	
	BTC_respawn_marker = format ["respawn_%1",playerSide];
	if (BTC_respawn_marker == "respawn_guer") then {BTC_respawn_marker = "respawn_guerrila";};
	BTC_r_base_spawn = "Land_HelipadEmpty_F" createVehicleLocal getMarkerPos BTC_respawn_marker;
	if (BTC_pvp == 1) then {if (BTC_respawn_marker == "respawn_civ") then {BTC_respawn_marker = "respawn_civilian";};};
	
	if (BTC_r_new_system == 0) then 
	{
		player addEventHandler ["Killed", FUNC(playerKilled)];
		if (BTC_respawn_gear == 1 && BTC_gear_on_death == 0) then {
			_idx = player addEventHandler ["HandleDamage", FUNC(handleDamageGear)];
			// Not needed anymore, fixed by BIS
			//["PLAYER_HANDLEDAMAGE",FUNC(handleDamageGear)] call tyr_fnc_addHandler;
			// 0 spawn {
				// sleep 1.5;
				// player removeAllEventHandlers "handleDamage";
				// _idx = player addEventHandler ["HandleDamage", FUNC(handleDamageGear)];
				// TRACE_1("Handler Damage EH",_idx);
			// };
		};
	} 
	else 
	{
		"BTC_cpr_pveh" addPublicVariableEventHandler BTCR_fnc_PVEH;
		"BTC_ban_pveh" addPublicVariableEventHandler BTCR_fnc_PVEH;
		"BTC_med_pveh" addPublicVariableEventHandler BTCR_fnc_PVEH;
		"BTC_anim_pveh" addPublicVariableEventHandler BTCR_fnc_PVEH;
		player setVariable ["BTC_r_status",[0,0,0,0,0],true];
		_idx = player addEventHandler ["HandleDamage", FUNC(handledamage)];
		_idx = player addEventHandler ["GetIn", FUNC(getInGear)];
		// Not needed anymore, fixed by BIS
		//["PLAYER_HANDLEDAMAGE",FUNC(handledamage)] call tyr_fnc_addHandler;
		// 0 spawn {
			// sleep 1.5;
			// player removeAllEventHandlers "handleDamage";
			// _idx = player addEventHandler ["HandleDamage", FUNC(handledamage)];
			// TRACE_1("Handler Damage EH",_idx);
		// };
		player addEventHandler ["Killed", FUNC(ehKilled)];
		BIS_fnc_healthEffects_handleDamage_code = {};
		BIS_fnc_healtEffects_init = {};
		BTC_r_action_menu = true;
		BTC_r_med_effect = false;
		BTC_is_bleeding = false;
		enableCamShake true; 
		BTC_r_unc = false;
		BTC_r_unc_loop = false;
		BTC_r_damage = 0;
		BTC_r_head = 0;
		BTC_r_damage_legs = 0;
		BTC_r_damage_hands = 0;
		BTC_r_hit = 0;
	};
	player setVariable ["BTC_need_revive",0,true];
	//{_x setVariable ["BTC_need_revive",0,true];} foreach allunits;//[] spawn {while {true} do {sleep 0.1;player sidechat format ["%1",BTC_r_mobile_selected];};};
	if (BTC_pvp == 1) then {player setVariable ["BTC_revive_side",str(BTC_side),true];};
	player setVariable ["BTCR_dragged",0,true];
	[] call FUNC(assignActions);
	
	if (BTC_active_mobile == 1) then 
	{
		[] spawn FUNC(mobileMarker);		
		_side = BTC_Side_Str;
		_str_array = format["BTC_objects_actions_%1",_side];
		_array = missionNamespace getVariable [_str_array,[]];
		for "_i" from 0 to ((count _array) - 1) step 1 do {
			private ["_veh"];
			_veh = _array select _i;
			[_veh] call FUNC(addTeleportAction);
			TRACE_1("Initialized Teleport",ARR_2(_veh, vehicleVarName _veh));
		};
		for "_i" from 0 to (49) step 1 do {
			private ["_veh"];
			_var_name = format["BTC_objects_actions_%1_%2",_side,_i];
			_veh = missionNameSpace getVariable [_var_name,objNull];
			if (!isnil "_veh" && {typename _veh == typename objNull} && {!isnull _veh}) then {
				[_veh] call FUNC(addTeleportAction);
				TRACE_1("Initialized Teleport",_var_name);
			};
		};		
	};
	BTC_gear = [] call FUNC(getGear);
	if (BTC_loop_check == 1) then {[] spawn FUNC(reviveLoop);};
	if (({player isKindOf _x} count BTC_3d_can_see) > 0) then {if (BTC_pvp == 1) then {_3d = [] spawn FUNC(3DMarkersPvP);} else {_3d = [] spawn FUNC(3DMarkers);};};
	//[] spawn {while {true} do {sleep 0.5;hintSilent format ["%1",BTC_gear];};};
	BTC_revive_started = true;
	
	waituntil {!isnil "BTC_vehs_mobile_civ_str"};
	if (BTC_active_mobile == 1 && {(BTC_mobile_teleport == 1 || BTC_mobile_heal == 1 || BTC_mobile_ammo == 1)}) then 
	{
		private ["_vehicles"];
		_side = BTC_Side_Str;
		_str_array = format["BTC_vehs_mobile_%1_str",_side];
		_array = missionNamespace getVariable [_str_array,[]];
		_vehicles = vehicles + (allMissionObjects "FlagCarrierCore");
		TRACE_1("Possible Vehicles",count _vehicles);
		{
			_vec = _x;
			_var_name = _vec getVariable [("BTC_mobile_" + _side),""];
			if (_var_name != "") then {
				if (BTC_mobile_teleport == 1) then {[_vec] call FUNC(addTeleportAction);};
				if (BTC_mobile_heal == 1) then {[_vec] call FUNC(addHealAction);};
				if (BTC_mobile_ammo == 1) then {[_vec] call FUNC(addAmmoAction);};
				TRACE_1("Initialized Vec",_var_name);
			};
		} foreach _vehicles;
	};
	
	TRACE("REVIVE STARTED");
};